package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.updateMsg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.JsmUpdateInfoMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.JsmUpdateInfo;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.JsmUpdateMsgMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.JsmUpdateMsg;
import com.cqhilink.iot.jobstandardmanagement.facade.service.updateMsg.JsmUpdateMsgService;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @Author 卢曼成
 * @Description
 * @CreateDateTime 2017-07-31 11:44
 **/
public class JsmUpdateMsgServiceImpl implements JsmUpdateMsgService {
    @Autowired
    private JsmUpdateMsgMapper jsmUpdateMsgMapper;
    @Autowired
    private JsmUpdateInfoMapper jsmUpdateInfoMapper;

    /**
     * 物理删除
     *
     * @param upDateId
     * @return
     */
    @Override
    public Map<String, Object> delete(String upDateId) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(upDateId)) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.DEL_FAIL);
        }
        int num = jsmUpdateMsgMapper.deleteByPrimaryKey(upDateId);
        if (num > 0) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.DEL_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.DEL_FAIL);
        }
        return map;
    }

    /**
     * 批量物理删除
     *
     * @param upInfoIds
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> deleteList(String upInfoIds) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        if (StringUtils.isEmpty(upInfoIds)) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", "参数错误!");
        }
        List<String> list = JSON.parseArray(upInfoIds, String.class);
        int num = jsmUpdateMsgMapper.deleteListKey(list);
        if (num > 0) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.DEL_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.DEL_FAIL);
        }
        return map;
    }

    /**
     * 添加
     *
     * @param record
     * @return
     */
    @Override
    public Map<String, Object> save(JsmUpdateMsg record) {
        record.setUpDateId(UniqueUtil.uuid());
        int num = jsmUpdateMsgMapper.insertSelective(record);
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0l) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.ADD_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.ADD_FAIL);
        }
        return map;
    }

    /**
     * 根据ID查询
     *
     * @param upDateId
     * @return
     */
    @Override
    public JsmUpdateMsg selectByPrimaryKey(String upDateId) {
        if (StringUtils.isEmpty(upDateId)) {
            return null;
        }
        return jsmUpdateMsgMapper.selectByPrimaryKey(upDateId);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     */
    @Override
    public Map<String, Object> update(JsmUpdateMsg record) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(record.getUpDateId())) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.MODIFY_FAIL);
        }
        int num = jsmUpdateMsgMapper.updateByPrimaryKeySelective(record);
        if (num > 0) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.MODIFY_OK);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.MODIFY_FAIL);
        }
        return map;
    }

    /**
     * 分页查询
     *
     * @param record
     * @return
     */
    @Override
    public Map<String, Object> queryList(JsmUpdateMsg record) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<JsmUpdateMsg> list = jsmUpdateMsgMapper.queryList(record);
        int count = jsmUpdateMsgMapper.queryCount(record);
        map.put("total", count);
        map.put("rows", list);
        return map;
    }

    /**
     * 查询更新版本
     *
     * @return
     */
    @Override
    public List<JsmUpdateMsg> messageList() {
        List<JsmUpdateMsg> list = jsmUpdateMsgMapper.selectByParamList(null);
        if (list == null || list.size() == 0)
            return null;

        List<JsmUpdateInfo> infoList = jsmUpdateInfoMapper.selectByParamList(null);
        if (infoList == null || infoList.size() == 0)
            return list;

        for (JsmUpdateMsg jum : list) {
            Iterator<JsmUpdateInfo> it = infoList.iterator();
            List<JsmUpdateInfo> newLists = new ArrayList<JsmUpdateInfo>();
            List<JsmUpdateInfo> repairList = new ArrayList<JsmUpdateInfo>();
            while (it.hasNext()) {
                JsmUpdateInfo jui = it.next();
                if (jui.getUpMsgId().equals(jum.getUpDateId())) {
                    if ("1".equals(jui.getUpType()) || jui.getUpType() == 1) {
                        newLists.add(jui);
                    } else if ("2".equals(jui.getUpType()) || jui.getUpType() == 2) {
                        repairList.add(jui);
                    }
                    it.remove();
                }
            }
            jum.setNewList(newLists);
            jum.setRepairList(repairList);
        }
        return list;
    }


}
